<?php

    class SHEDULE_DATETIMELIST_ITEM
    {
        public $id = '';
        public $time = '';
        public $date = '';
        public $status = '';
        public $place; // номер кабинета
    }

    class SHEDULE_DATETIMELIST
    {
        public $o_dtlitems = Array();
        function count()
        {
            return count($this->o_dtlitems);
        }
        function items()
        {
            return $this->o_dtlitems;
        }
        function item($index)
        {
            return $this->o_dtlitems[$index];
        }
        function add($id, $date, $time, $status, $place)
        {
            $dtlitem = new SHEDULE_DATETIMELIST_ITEM();
            $dtlitem->id = $id;
            $dtlitem->date = $date;
            $dtlitem->time = $time;
            $dtlitem->status = $status;
            $dtlitem->place = $place;
            $this->o_dtlitems[] = $dtlitem;
            return $dtlitem;
        }
        function FreeTicketsCount($date)
        {
            $freetickets = 0;
            foreach($this->o_dtlitems as $ticket)
            {
                if($ticket->date==$date)
                {
                    if($ticket->status==0){$freetickets++;}
                }
            }
            return $freetickets;
        }
    }

    class SHEDULE_ITEM
    {
        public $id = "";
        public $fio = "";
        public $speciality = "";
        public $state = "";
        public $division = "";
        public $datetimelist = "";
        public function __construct()
        {
            $this->datetimelist = new SHEDULE_DATETIMELIST();
        }
    }

    class SHEDULE
    {

        public $o_items = Array();

        function count()
        {
            return count($this->o_items);
        }

        function items()
        {
            return $this->o_items;
        }

        function item($index)
        {
            return $this->o_items[$index];
        }

        function itembyid($postid)
        {
            reset($this->o_items);
            foreach($this->o_items as $item)
            {
                if($item->id == $postid)
                {
                    return $item;
                }
            }
        }

        function add($id, $fio, $speciality, $state, $division)
        {
            $item = new SHEDULE_ITEM();
            $item->id = $id;
            $item->fio = $fio;
            $item->speciality = $speciality;
            $item->state = $state;
            $item->division = $division;
            $this->o_items[] = $item;
            return $item;
        }
    }

    // Экземпляр класса расписания врачей
    $ched = new SHEDULE();

    function LoadShedules($postid, $datebegin, $lpu = null, $dateend = null)
    {
        global $ched;
        $post_ids = Array();
        if($postid != null) {$post_ids[] = $postid;}
        $xml = CreateGetPostsXMLInfomat($post_ids, $datebegin, $dateend);
        $xmlfile = CallSoap('getpostlist', $xml);

        // XML с данными расписания
        $dom1 = new DomDocument();
        $dom1->loadXML($xmlfile);
        //$dom1->encoding = 'Utf-16'; //'windows-1251';
        $schedules = $dom1->getElementsByTagName("schedule");
        // Заполнение классов из XML
        foreach($schedules as $schedule)
        {
            $posts = $schedule->getElementsByTagName("post");
            foreach($posts as $post)
            {
                $id = $post->getAttribute('guid');
                $fio = $post->getAttribute('fio');
                $state = $post->getAttribute('state');
                $speciality = $post->getAttribute('speciality');
                $division = $post->getAttribute('division');
                // $fio = i conv("UTF-8", "WINDOWS-1251", $fio);
                // $speciality = i conv("UTF-8", "WINDOWS-1251", $speciality);
                // $division = i conv("UTF-8", "WINDOWS-1251", $division);
                $item = $ched->add($id, $fio, $speciality, $state, $division);
                $dtl = $item->datetimelist;
                $tickets = $schedule->getElementsByTagName("tickets");
                foreach($tickets as $ticketArray)
                {
                    $ticket = $ticketArray->getElementsByTagName("ticket");
                    foreach($ticket as $tkt)
                    {
                        $id = $tkt->getAttribute('guid');
                        $date = $tkt->getAttribute('date');
                        $time = $tkt->getAttribute('time');
                        $status = $tkt->getAttribute('status');
                        $place = $tkt->getAttribute('place');
                        // $place = i conv("UTF-8", "WINDOWS-1251", $place);
                        $dtl->add($id, $date, $time, $status, $place);
                    }
                }
            }
        }
    }

    function getpostfio($postid)
    {
        global $ched;
        foreach($ched->items() as $item)
        {
            if($item->id == $postid)
            {
                return $item->fio."<br>".$item->speciality;
            }
        }
    }

?>